package system;

import java.util.ArrayList;

public class Game {

	Board b;
	ArrayList<Player> players = new ArrayList<Player>();
	
	public Game(){
		b = new Board();
	Player player = new Player(Color.RED, "immonius");
	players.add( player);
	Player player2 = new Player(Color.PURPLE, "JannesVU");
	players.add( player2);
	Player player3 = new Player(Color.GREEN, "MikeyMaster");
	players.add( player3);
	
	ActionPoint actionPoint = b.getPointAt(6.25,5.0);
	actionPoint.assign(player);
	player.addHouse(actionPoint);
	
	
	
	ActionPoint actionPoint2 =  b.getPointAt(4.75,5.0);
	actionPoint2.assign(player2);
	player2.addHouse(actionPoint2);
	
	ActionPoint actionPoint3 = b.getPointAt(4.75,4);
	actionPoint3.assign(player3);
	player3.addHouse(actionPoint3);
	
	for (Edge e :b.getEdges(actionPoint)) {
		addUnrestrictedEdge(e, player);
	}
	for (Edge e :b.getEdges(actionPoint2)) {
		addUnrestrictedEdge(e, player2);
	}
	for (Edge e :b.getEdges(actionPoint3)) {
		addUnrestrictedEdge(e, player3);
	}
	
	}

	
	
	
	public ArrayList<Player> getPlayers() {
		return players;
	}
	
	
	public void addHouse(ActionPoint ap,Player p){
		if (ap.getOwner() == null){
			boolean connected = false;
			
			ArrayList<ActionPoint> neighbours = b.getNeighbours(ap);
			
			System.out.println(neighbours);
			for (ActionPoint app : neighbours) {
				app = b.getPoint(app);
				if (app.getOwner()!= null)
					return;
			}
			
			
			ArrayList<Edge> edges = b.getEdges(ap);
			//System.out.println("edges: " + edges.size());
			for (Edge e : edges) {
				
				if (e.getOwner() != null && e.getOwner().equals(p)){
					connected = true;
				}
			}
			
			if (connected){
			ap.assign(p);
		p.addHouse(ap);
			}
		
		
		}
	}
	
	public boolean addEdge(Edge e,Player p){
		if (e.getOwner() == null && !e.waterway){
			
			
			
			boolean connected = false;
			for (Edge  ed : b.getEdges(e.getP1())) {
				if (!(ed.getOwner() == null)){
				if (ed.getOwner().equals(p))
					connected = true;
				}
			}
			
			for (Edge  ed : b.getEdges(e.getP2())) {
				if (!(ed.getOwner() == null)){
					if (ed.getOwner().equals(p))
						connected = true;
					}
			}
			
				if (connected){
					e.assign(p);
					p.addStreet(e);
					return true;
				}
			}
		return false;
	}
	
	
	public boolean addUnrestrictedEdge(Edge e,Player p){
		if (e.getOwner() == null && !e.waterway){
			
			
			
		
					e.assign(p);
					p.addStreet(e);
					return true;
				
			}
		return false;
	}
	
	public Board getBoard(){
		return b;
	}
	
}
